17.1 Introduction to the Task Scheduler
The task scheduler serves as the core of the Linux runtime environment and is the definitive kernel component responsible for allocating CPU execution cycles to individual processes or threads. It dictates which task runs at what time, how long it retains control, and the exact moment it is stripped of its execution rights via preemption.
The Linux scheduler has undergone several major architectural iterations over time, progressing from the early (O(1)) scheduler to modern fairness-based frameworks. The Completely Fair Scheduler (CFS) served as the default Linux scheduling algorithm for a substantial epoch. By tracking and maintaining a "virtual runtime" (vruntime) parameter for each active task, CFS strived to split and distribute CPU execution time proportionally and fairly among competing workloads.
However, over time, the continuous accumulation of modifications intended to safeguard task fairness caused the CFS codebase to grow exceedingly complex. To overcome this complexity barrier, the Earliest Eligible Virtual Deadline First (EEVDF) scheduling algorithm was introduced. EEVDF has since systematically superseded the legacy CFS framework. While preserving the underlying principles of computational fairness, EEVDF incorporates the microarchitectural concept of a "virtual deadline," allowing the engine to better balance high-throughput workloads against low-latency demands, particularly optimizing performance for latency-sensitive tasks.
Linux utilizes a modular, hierarchical scheduling architecture that segregates tasks into distinct scheduling classes ranked by strict priority boundaries. During a scheduling pass, the core engine consults each scheduling class sequentially from the highest priority downward until it resolves a runnable task. The task scheduler orchestrates these operations via abstraction interfaces defined by the sched_class structure. As declared within the core header file git/kernel/sched/sched.h, Linux partitions tasks into the following scheduling classes:
-
Stop Scheduling Class (stop_sched_class):
The highest-priority class in the system, utilized to forcefully halt all concurrent operational threads executing on a given CPU core. It is primarily deployed for critical machine-level procedures such as CPU hotplug events, cluster task migrations, and kernel panics or shutdowns. It preempts any running workload instantly; even the highest-priority real-time tasks must yield execution boundaries to it. -
Deadline Scheduling Class (dl_sched_class):
The highest-priority class among standard executable workloads, implementing the SCHED_DEADLINE scheduling policy. It is geared exclusively toward hard real-time application profiles, computing execution routes based on explicit runtime budgets, deadlines, and periodic execution windows configured for the target task. -
Real-Time Scheduling Class (rt_sched_class):
Implementing the SCHED_FIFO (First-In, First-Out) and SCHED_RR (Round-Robin) real-time scheduling policies. Unless preempted by a higher-priority thread or voluntarily relinquishing control of the CPU, a task executing under the First-In, First-Out paradigm runs continuously. Conversely, under the Round-Robin paradigm, tasks of identical priority share execution windows, and any task that exhausts its assigned time slice is shunted to the tail boundary of the local execution queue. -
Fair Scheduling Class (fair_sched_class):
Governing standard workloads via the SCHED_NORMAL, SCHED_BATCH, and SCHED_IDLE scheduling policies. These policies map respectively to standard interactive application threads, non-interactive compute-bound workloads, and background tasks designated to execute exclusively when the system rests in a completely vacant idle state.
The kernel leverages timer hardware interrupts to monitor whether the currently executing task has exhausted its allocated runtime allocation. When the time slot is spent, the kernel invokes the task scheduler to switch execution context over to a newly resolved task. Furthermore, an active task can voluntarily yield its assigned CPU execution time by calling functions such as sleep(). Linux natively supports full task preemption; the moment a high-priority task is awakened and transitions into a runnable ready state, the kernel immediately preempts and evicts the active low-priority workload, instantly executing the ready high-priority task to guarantee highly responsive, low-latency execution paths.
The Linux kernel maintains a unified internal priority spectrum scaling from 0 to 139, where smaller numerical values represent higher architectural priorities. The priority band spanning 0 to 99 is reserved for real-time priorities, which maps directly to user-space configurations scaling from 1 to 99. Concurrently, Linux maintains a localized nice range scaling from -20 to +19, enabling users to fine-tune the relative priority levels of standard tasks.
To optimize scalability, Linux allocates an independent, private execution runqueue to each individual CPU core. The scheduling subsystem continuously monitors these execution tracks and dynamically migrates tasks across cores to enforce load balancing across the entire multi-core hardware infrastructure.
The routine sched_init() represents a critical initialization function in the Linux kernel boot sequence. Its primary technical objective is to establish the baseline data structures and operational runtime environments consumed by the process scheduler, ensuring the operating system can reliably manage and switch execution contexts across different tasks. This function is hosted within the source file git/kernel/sched/core.c, and its definition is implemented as:
void __init sched_init(void)
{
unsigned long ptr = 0;
int i;
BUG_ON(&idle_sched_class + 1 != &fair_sched_class
|| &fair_sched_class + 1 != &rt_sched_class
|| &rt_sched_class + 1 != &dl_sched_class);
#ifdef CONFIG_SMP
BUG_ON(&dl_sched_class + 1 != &stop_sched_class);
#endif
wait_bit_init();
#ifdef CONFIG_FAIR_GROUP_SCHED
ptr += 2 * nr_cpu_ids * sizeof(void **);
#endif
#ifdef CONFIG_RT_GROUP_SCHED
ptr += 2 * nr_cpu_ids * sizeof(void **);
#endif
if (ptr) {
ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT);
#ifdef CONFIG_FAIR_GROUP_SCHED
root_task_group.se = (struct sched_entity **)ptr;
ptr += nr_cpu_ids * sizeof(void **);
root_task_group.cfs_rq = (struct cfs_rq **)ptr;
ptr += nr_cpu_ids * sizeof(void **);
root_task_group.shares = ROOT_TASK_GROUP_LOAD;
init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
#endif /* CONFIG_FAIR_GROUP_SCHED */
#ifdef CONFIG_RT_GROUP_SCHED
root_task_group.rt_se = (struct sched_rt_entity **)ptr;
ptr += nr_cpu_ids * sizeof(void **);
root_task_group.rt_rq = (struct rt_rq **)ptr;
ptr += nr_cpu_ids * sizeof(void **);
#endif /* CONFIG_RT_GROUP_SCHED */
}
#ifdef CONFIG_CPUMASK_OFFSTACK
for_each_possible_cpu(i) {
per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
cpumask_size(), GFP_KERNEL, cpu_to_node(i));
per_cpu(select_idle_mask, i) = (cpumask_var_t)kzalloc_node(
cpumask_size(), GFP_KERNEL, cpu_to_node(i));
}
#endif /* CONFIG_CPUMASK_OFFSTACK */
init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime());
init_dl_bandwidth(&def_dl_bandwidth, global_rt_period(), global_rt_runtime());
#ifdef CONFIG_SMP
init_defrootdomain();
#endif
#ifdef CONFIG_RT_GROUP_SCHED
init_rt_bandwidth(&root_task_group.rt_bandwidth,global_rt_period(), global_rt_runtime());
#endif /* CONFIG_RT_GROUP_SCHED */
#ifdef CONFIG_CGROUP_SCHED
task_group_cache = KMEM_CACHE(task_group, 0);
list_add(&root_task_group.list, &task_groups);
INIT_LIST_HEAD(&root_task_group.children);
INIT_LIST_HEAD(&root_task_group.siblings);
autogroup_init(&init_task);
#endif /* CONFIG_CGROUP_SCHED */
for_each_possible_cpu(i) {
struct rq *rq;
rq = cpu_rq(i);
raw_spin_lock_init(&rq->lock);
rq->nr_running = 0;
rq->calc_load_active = 0;
rq->calc_load_update = jiffies + LOAD_FREQ;
init_cfs_rq(&rq->cfs);
init_rt_rq(&rq->rt);
init_dl_rq(&rq->dl);
#ifdef CONFIG_FAIR_GROUP_SCHED
INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
#endif /* CONFIG_FAIR_GROUP_SCHED */
rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
#ifdef CONFIG_RT_GROUP_SCHED
init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
#endif
#ifdef CONFIG_SMP
rq->sd = NULL;
rq->rd = NULL;
rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
rq->balance_callback = NULL;
rq->active_balance = 0;
rq->next_balance = jiffies;
rq->push_cpu = 0;
rq->cpu = i;
rq->online = 0;
rq->idle_stamp = 0;
rq->avg_idle = 2*sysctl_sched_migration_cost;
rq->max_idle_balance_cost = sysctl_sched_migration_cost;
INIT_LIST_HEAD(&rq->cfs_tasks);
rq_attach_root(rq, &def_root_domain);
#ifdef CONFIG_NO_HZ_COMMON
rq->last_blocked_load_update_tick = jiffies;
atomic_set(&rq->nohz_flags, 0);
rq_csd_init(rq, &rq->nohz_csd, nohz_csd_func);
#endif
#endif /* CONFIG_SMP */
hrtick_rq_init(rq);
atomic_set(&rq->nr_iowait, 0);
}
set_load_weight(&init_task, false);
mmgrab(&init_mm);
enter_lazy_tlb(&init_mm, current);
init_idle(current, smp_processor_id());
calc_load_update = jiffies + LOAD_FREQ;
#ifdef CONFIG_SMP
idle_thread_set_boot_cpu();
#endif
init_sched_fair_class();
init_schedstats();
psi_init();
init_uclamp();
scheduler_running = 1;
}
The diverse scheduling classes are stored in memory in a fixed sequence. Arranged in ascending order of their physical memory addresses, the storage sequence of the scheduling classes maps precisely to idle_sched_class, fair_sched_class, rt_sched_class, dl_sched_class, and stop_sched_class.